package com.hitqz.robot.biz.flow.component.patrol;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.enums.EquipmentEnum;
import com.hitqz.robot.api.business.enums.RobotOtherCommandEnum;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolTicketsDto;
import com.hitqz.robot.api.common.entity.RobotEquipment;
import com.hitqz.robot.api.common.entity.RobotMapPos;
import com.hitqz.robot.api.common.entity.TicketsTask;
import com.hitqz.robot.biz.service.RobotEquipmentService;
import com.hitqz.robot.biz.service.RobotMapPosService;
import com.hitqz.robot.camera.EquipmentFactory;
import com.hitqz.robot.camera.IEquipmentService;
import com.hitqz.robot.camera.configure.EquipmentCmdEnum;
import com.hitqz.robot.camera.dto.QueryParamsDto;
import com.hitqz.robot.driver.model.IRobotPosDto;
import com.hitqz.robot.driver.parent.RobotDriverService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Objects;

@Slf4j
@LiteflowComponent(value = "TurnJackingOriginByPosition", name = "[巡检]根据位置回正顶升")
@AllArgsConstructor
public class TurnJackingOriginByPosition extends NodeComponent {

    private final RobotEquipmentService robotEquipmentService;

    private final EquipmentFactory equipmentFactory;

    private final RobotMapPosService robotMapPosService;

    private final RobotDriverService robotDriverService;


    @Override
    public void process() throws Exception {
        FlowContextDto dto =  this.getRequestData();
        if (isNeedSkip(dto)){
            log.info("跳过当前步骤");
            return;
        }
        TicketsTask task = dto.getTicketsTask();
        log.info("任务数据 {}", task.getParseOrderParams());
        FlowPatrolTicketsDto params = JSONObject.parseObject(task.getParseOrderParams(), FlowPatrolTicketsDto.class);
        FlowPatrolPreAndEndTicketsDto endParams = JSONObject.parseObject(task.getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        log.info("{} ---params: {}", task.getParseOrderParams(), JSONObject.toJSONString(params));
        if (params.getPosId()==null && endParams.getEndPosId()==null){
            log.warn("没有站点信息,不执行");
            return;
        }
        Integer posId = params.getPosId() != null ? params.getPosId() : endParams.getEndPosId();
        RobotMapPos robotMapPos = robotMapPosService.getById(posId);
        IRobotPosDto iRobotPosDto = robotDriverService.getWebPos();
        if (Objects.equals(iRobotPosDto.getCurrentStation(), robotMapPos.getCode()) && iRobotPosDto.isReached()) {
            log.info("同个点位{},无需回正",robotMapPos.getCode());
            return;
        }
        List<RobotEquipment> robotEquipments = robotEquipmentService.list(Wrappers.<RobotEquipment>lambdaQuery()
                .eq(RobotEquipment::getType, EquipmentEnum.ROBOT.getType())
                .eq(RobotEquipment::getIsRecordParam, 1));
        for (RobotEquipment equipment : CollUtil.emptyIfNull(robotEquipments)) {
            robotDriverService.executeOtherCommand(RobotOtherCommandEnum.SET_JACKING_ORIGIN,null);
        }

    }


    private boolean isNeedSkip(FlowContextDto dto){
        try {
            TicketsTask lastTicketsTask = dto.getLastTicketsTask();
            if (lastTicketsTask == null) {
                return false;
            }
            //当前参数
            TicketsTask task = dto.getTicketsTask();
            FlowPatrolTicketsDto params = JSONObject.parseObject(task.getParseOrderParams(), FlowPatrolTicketsDto.class);
            FlowPatrolPreAndEndTicketsDto endParams = JSONObject.parseObject(task.getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
            Integer currentTargetPosId = params.getPosId() != null ? params.getPosId() : (endParams.getEndPosId() != null ? endParams.getEndPosId() : null);
            //上一次的参数
            FlowPatrolTicketsDto lastParams = JSONObject.parseObject(lastTicketsTask.getParseOrderParams(), FlowPatrolTicketsDto.class);
            FlowPatrolPreAndEndTicketsDto lastEndParams = JSONObject.parseObject(lastTicketsTask.getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
            Integer lastTargetPosId = lastParams.getPosId() != null ? lastParams.getPosId() : (lastEndParams.getEndPosId() != null ? lastEndParams.getEndPosId() : null);
            if (Objects.equals(currentTargetPosId,lastTargetPosId)){
                log.info("当前工单任务与上一次工单任务点位相同");
                return true;
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }
}